package com.nlscan.ncomgateway.core.newland.helper

import android.text.TextUtils
import com.google.gson.Gson
import com.nlscan.ble.NlsBleManager
import com.nlscan.ble.util.NLogUtil
import com.nlscan.ncomgateway.core.newland.entity.NlsIpCommand

object NlsIpCommandHelper {

    fun parse(input: String): NlsIpCommand? {

        val gson = Gson()
        var command: NlsIpCommand? = null
        try {
            command = gson.fromJson(input, NlsIpCommand::class.java)
        } catch (e: Exception) {
            NLogUtil.e("NlsIpCommandHelper parse error!!")
        }

        return command
    }


    fun sendIpCommand(ipCommand:NlsIpCommand?) {
        NLogUtil.i( "sendIpCommand start")
        if (ipCommand == null) {
            NLogUtil.i( "sendIpCommand ipCommand is null")
        }

        //当前只支持单个mac
        val extraAddress = ipCommand?.id
        val extraCmd = ipCommand?.cmd
        val bleManager = NlsBleManager.getInstance()


//        val addressList = mutableListOf<String>()
        if (TextUtils.isEmpty(extraAddress)) {
            //mac(id)值为空，则向所有设备发送
            val connections = bleManager.connections

            if (connections.isEmpty()) {
//                AppBroadcastManager.sendReceiveCmdResultBroadcast(
//                    this, extraAddress, extraCmd, AppErrStatus.ERR_CONN_NO_DEVICE
//                )
            } else {
                // 查询所有设备
                for (connection in connections.values) {
//                    val address = connection?.device?.address
                    if (TextUtils.isEmpty(extraCmd)) {
                        // 1 指令为空
//                        AppBroadcastManager.sendReceiveCmdResultBroadcast(
//                            this, address, extraCmd, AppErrStatus.ERR_CMD_EMPTY
//                        )
                    } else {
                        // 发送指令
                        connection.sendNlsCmd(extraCmd)

//                        connection.sendNlsCmd(extraCmd) { cmd, nlsResult ->
//                            NLogUtil.dTag(
//                                "TAG41",
//                                "sendIpCommand sendNlsCmd status: ${nlsResult.code}, result: ${nlsResult.result}"
//                            )
//                        }
                    }
                }
            }
        } else {
//            extraAddress?.split(";")?.let {
//                for (address in it) {
//                    val connection = bleManager.getConnection(address)
//                    if (connection == null) {
//
////                        sendErrorCmdResultBroadcast(address,extraCmd)
//                    }
//                }
//            }

            val connections = bleManager.connections
            if (connections.isEmpty()) {
//                AppBroadcastManager.sendReceiveCmdResultBroadcast(
//                    this, extraAddress, extraCmd, AppErrStatus.ERR_CONN_NO_DEVICE
//                )
            } else {
                // 查询所有设备
                for (connection in connections.values) {
                    val address = connection?.device?.address
                    if (TextUtils.isEmpty(address) || !address.equals(extraAddress)) {
                        continue
                    }

                    if (TextUtils.isEmpty(extraCmd)) {
                        // 1 指令为空
//                        AppBroadcastManager.sendReceiveCmdResultBroadcast(
//                            this, address, extraCmd, AppErrStatus.ERR_CMD_EMPTY
//                        )
                    } else {
                        // TODO: 当前带回调发送指令会引起重复发送问题，临时改为不关心回调
                        // 发送指令
                        connection.sendNlsCmd(extraCmd)

//                        connection.sendNlsCmd(extraCmd) { cmd, nlsResult ->
//                            NLogUtil.dTag(
//                                "TAG41",
//                                "sendIpCommand sendNlsCmd status: ${nlsResult.code}, result: ${nlsResult.result}"
//                            )
//                        }
                    }
                }
            }
        }
    }


}